﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;

namespace Sl.Caching
{
    /// <summary>
    /// 缓存依赖工厂
    /// </summary>
    public class DependencyAccess
    {
        /// <summary>
        /// 创建普通缓存
        /// </summary>
        /// <typeparam name="TKey">键类型</typeparam>
        /// <typeparam name="TResult">返回对象类型</typeparam>
        /// <returns></returns>
        public static IDependCache<TKey, TResult> CreateValueDependency<TKey, TResult>()
        {
            return LoadInstance<TKey, TResult>(new DependValueCache<TKey, TResult>().GetType(), null);
        }

        private static IDependCache<TKey, TResult> LoadInstance<TKey, TResult>(Type type, ICachingSection isetion)
        {
            //string path = "Sl.Caching";
            //string classNames = path + "." + className + "`2[[" + typeof(TKey).FullName + "," + typeof(TKey).Assembly.ToString() + "],[" + typeof(TResult).FullName + "," + typeof(TResult).Assembly.ToString() + "]]";
            //Assembly assembly = Assembly.Load(path);
            //Type t = assembly.GetType(classNames);
            if (isetion != null)
            {
                object[] args = new object[1];
                args[0] = isetion;
                return (IDependCache<TKey, TResult>)Activator.CreateInstance(type, args);
            }
            else
            {
                return (IDependCache<TKey, TResult>)Activator.CreateInstance(type);
            }
        }

        //private static IDependCache<TKey, TResult> LoadInstance<TKey, TResult>(string className, ICachingSection isetion)
        //{
        //    string path = "Sl.Caching";
        //    string classNames = path + "." + className + "`2[[" + typeof(TKey).FullName + "," + typeof(TKey).Assembly.ToString() + "],[" + typeof(TResult).FullName + "," + typeof(TResult).Assembly.ToString() + "]]";
        //    Assembly assembly = Assembly.Load(path);
        //    Type t = assembly.GetType(classNames);
        //    if (isetion != null)
        //    {
        //        object[] args = new object[1];
        //        args[0] = isetion;
        //        return (IDependCache<TKey, TResult>)Activator.CreateInstance(t, args);
        //    }
        //    else
        //    {
        //        return (IDependCache<TKey, TResult>)Activator.CreateInstance(t);
        //    }
        //}
    }
}
